﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JRFinances.Models;
using JRFinances.Business;
using PagedList;
using JRFinances.ViewModels;
using AutoMapper;
using JRFinances.ViewModels.Companies;

namespace JRFinances.Controllers
{
    public class CompaniesController : Controller
    {
        private JRFinancesEntities db = new JRFinancesEntities();

        public ActionResult Index(int? page)
        {
            var companies = db.Companies.OrderBy(x => x.Name); // pobranie spolek z bazy danych i posortowanie po nazwie
            var companiesViewModel = new List<IndexCompanyViewModel>();

            companies.ToList().ForEach(n => companiesViewModel.Add(Mapper.Map<Company, IndexCompanyViewModel>(n))); // uzupelnienie modelu dla widoku pobranymi spolkami

            var pageNumber = page ?? 1;
            var onePageOfCompanies = companiesViewModel.ToPagedList(pageNumber, 20); // wybranie odpowiedniej strony dla stronnicowania

            return View(onePageOfCompanies);
        }

        [HttpPost]
        public ActionResult Search(string SearchedCompanyName)
        {
            var domainCompany = new Company();
            try
            {
                domainCompany = db.Companies.Single(n => n.Name == SearchedCompanyName);
            }
            catch
            {
                return RedirectToAction("Index");
            }
            DetailsCompanyViewModel company = Mapper.Map<DetailsCompanyViewModel>(domainCompany);

            return View("Details", company);
        }


        public ActionResult Details(int? id)
        {
            if (id.HasValue)
            {
                var domainCompany = db.Companies.Single(n => n.CompanyId == id);
                DetailsCompanyViewModel company = Mapper.Map<DetailsCompanyViewModel>(domainCompany);

                DateTime timeCondition = DateTime.Now.AddMonths(-12);
                company.NumberOfRealBuyTransactions = db.RealTransactions
                    .Where(n => n.Company.CompanyId == company.CompanyId
                        && n.Buy == true
                        && n.Date >= timeCondition).Count();

                return View(company);
            }
            else
            {
                return Index(0);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="option">0 - last 3 months, 1 - last year, 2 - last 3 years, 3 - last 10 years, 4 - all time</param>
        /// <returns></returns>
        public PartialViewResult DrawQuotationChart(int id, int? option)
        {
            QuotationChartViewModel quotationChart = new QuotationChartViewModel();
            DateTime limitData = GetLimitDataForQuotationChart(option ?? 4); // jesli nie wybrano zadnej opcji, pobierane sa wszystkie notowania
            quotationChart.XValues = db.Quotations.Where(n => n.CompanyId == id && n.Date >= limitData).OrderByDescending(n => n.Date).Select(n => n.Date).ToArray();
            quotationChart.YValues = db.Quotations.Where(n => n.CompanyId == id && n.Date >= limitData).OrderByDescending(n => n.Date).Select(n => n.Price).ToArray();

            return PartialView(quotationChart);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="option">0 - last 3 months, 1 - last year, 2 - last 3 years, 3 - last 10 years, 4 - all time</param>
        /// <returns></returns>
        private DateTime GetLimitDataForQuotationChart(int option)
        {
            DateTime limit = DateTime.Now;

            switch (option)
            {
                case 0: limit = limit.AddMonths(-3); break;
                case 1: limit = limit.AddYears(-1); break;
                case 2: limit = limit.AddYears(-3); break;
                case 3: limit = limit.AddYears(-10); break;
                case 4: limit = limit.AddYears(-100); break;
                default:
                    break;
            }

            return limit;
        }

    }
}
